package com.hemerasolutions.v2trainer.component.detail;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.ValueChangeEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.IntensityZoneListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.PhotosListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.VideosListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.table.EquipmentTableCompInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseDetailDelegateInterface;
import com.hemerasolutions.v2trainer.model.exercise.Exercise;
import com.hemerasolutions.v2trainer.model.exercise.ExerciseMuscle;
import com.hemerasolutions.v2trainer.model.exercise.VersionableExercise;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.muscle.BodyRegion;
import com.hemerasolutions.v2trainer.model.muscle.Muscle;
import com.hemerasolutions.v2trainer.model.muscle.MuscleGroup;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.service.interfaces.ModalityServiceInterface;


@Controller
@Component("exerciseDetailsComp")
@Scope("prototype")
public class ExerciseDetailsComp extends BaseComponent implements ExerciseDetailsCompInterface {

	private static final long serialVersionUID = 1L;

	@Autowired
	private VideosListCompInterface videosListHandler;

	@Autowired
	private PhotosListCompInterface photosListHandler;
	
	@Autowired
	private EquipmentTableCompInterface equipmentTable;
	
	@Autowired
	private IntensityZoneListCompInterface izList;
	
	private ExerciseDetailDelegateInterface delegate;
	
	private Exercise selectedVersion;
	
	private VersionableExercise versionableExercise;

	private Modality modality;
	
	private List<Exercise> exercises;
	
	private Exercise exercise;

	private List<BodyRegion> bodyRegions;
	
	private List<MuscleGroup> muscleGroups;
	
	private List<Muscle> muscles;
	
	private BodyRegion selBodyRegion;
	
	private MuscleGroup selMuscleGroup;
	
	private Muscle selMuscle;
	
	private Muscle muscle;
	
	private ExerciseMuscle exerciseMuscle;
	
	@Autowired
	private ModalityServiceInterface modalityService;
	
	private boolean musclePrimary;
	
	
	
	public ExerciseDetailsComp(){
		super();
	}
	
	@Override
	@PostConstruct
	public void init(){
		super.init();
		this.bodyRegions = this.modalityService.listBodyRegions();
	}
	
	public void cancelChange() {
		
	}

	@Override
	public void saveExercise() {

//		
//		boolean foundedVe = false;
//		boolean foundedEx = false;
//		
		if(this.exercise.getId() == null ){
			
			if(!this.modality.getVersionableExercises().contains(this.versionableExercise)){
				this.modality.getVersionableExercises().add(this.versionableExercise);
			}
			
			
//			for(int x = 0; x < this.modality.getVersionableExercises().size();x++){
//				if(this.modality.getVersionableExercises().get(x).getUuid() == this.versionableExercise.getUuid()){
//					foundedVe = true;
//					for(int y = 0; y < this.modality.getVersionableExercises().get(x).getExerciseVersions().size(); y++ ){
//						if(this.exercise.getUuid() == this.modality.getVersionableExercises().get(x).getExerciseVersions().get(y).getUuid()){
//							foundedEx = true;
//							this.modality.getVersionableExercises().get(x).getExerciseVersions().set(y, this.exercise);
//
//							if(this.modality.getVersionableExercises().get(x).getCurrentVersion().getUuid() == this.exercise.getUuid()){
//								this.modality.getVersionableExercises().get(x).setCurrentVersion(this.exercise);
//							}
//
//							break;
//						}
//					}
//				}
//				if(foundedEx){
//					break;
//				}
//			}
//			
//			if(!foundedVe){
//				this.modality.getVersionableExercises().add(this.versionableExercise);
//			}
//
//			if(!foundedEx){
//				for(int x = 0; x < this.modality.getVersionableExercises().size();x++){
//					if(this.modality.getVersionableExercises().get(x).getUuid() == this.versionableExercise.getUuid()){
//						this.modality.getVersionableExercises().get(x).getExerciseVersions().add(this.exercise);
//						this.modality.getVersionableExercises().get(x).setCurrentVersion(this.exercise);
//
//						if(this.modality.getVersionableExercises().get(x).getCurrentVersion().getUuid() == this.exercise.getUuid()){
//							this.modality.getVersionableExercises().get(x).setCurrentVersion(this.exercise);
//						}
//						break;	
//					}
//				}
//			}
			
		}else if(this.exercise.hasChanged(this.modalityService.getExerciseById(this.exercise.getId()))){
			if(this.exerciseCanBeChanged()){
//				for(int x = 0; x < this.modality.getVersionableExercises().size();x++){
//					if(this.modality.getVersionableExercises().get(x).getUuid() == this.versionableExercise.getUuid()){
//						foundedVe = true;
//						for(int y = 0; y < this.modality.getVersionableExercises().get(x).getExerciseVersions().size(); y++ ){
//							if(this.exercise.getUuid() == this.modality.getVersionableExercises().get(x).getExerciseVersions().get(y).getUuid()){
//								foundedEx = true;
//								this.modality.getVersionableExercises().get(x).getExerciseVersions().set(y, this.exercise);
//
//								if(this.modality.getVersionableExercises().get(x).getCurrentVersion().getUuid() == this.exercise.getUuid()){
//									this.modality.getVersionableExercises().get(x).setCurrentVersion(this.exercise);
//								}
//
//								break;
//							}
//						}
//					}
//					if(foundedEx){
//						break;
//					}
//				}
//				
//				if(!foundedVe){
//					this.modality.getVersionableExercises().add(this.versionableExercise);
//				}
//
//				if(!foundedEx){
//					for(int x = 0; x < this.modality.getVersionableExercises().size();x++){
//						if(this.modality.getVersionableExercises().get(x).getUuid() == this.versionableExercise.getUuid()){
//							this.modality.getVersionableExercises().get(x).getExerciseVersions().add(this.exercise);
//							this.modality.getVersionableExercises().get(x).setCurrentVersion(this.exercise);
//
//							if(this.modality.getVersionableExercises().get(x).getCurrentVersion().getUuid() == this.exercise.getUuid()){
//								this.modality.getVersionableExercises().get(x).setCurrentVersion(this.exercise);
//							}
//							break;	
//						}
//					}
//				}		
			}else{
				this.showWhereExerciseIsUsed();
				this.showNewVersionQuestion(this.exercise);
			
			}
		}else{
//			do nothing
		}

		if(this.delegate != null){
			this.delegate.onSaveVersionableExercise(versionableExercise);
		}
	}
	
	
	private void showNewVersionQuestion(Exercise exercise2) {
		this.executeJavascript("wNewExerciseVersionQuestion.show();");
		
	}
	
	@Override
	public void newExerciseVersion(){

		Exercise newVersion = new Exercise(this.exercise,this.modality); 

		newVersion.setVersion(this.versionableExercise.getExerciseVersions().size()+1);
	
		this.exercise = newVersion;
		this.refresh();
	}

	public boolean exerciseCanBeChanged(){
		if(this.exercise.getId() == null){
			return true;
		}else{
			if(this.modalityService.trainingsUsing(this.exercise).isEmpty()){
				return true;
			}else{
				return false;
			}
		}
	}

	
	public void showWhereExerciseIsUsed(){

		List<Training> trainings = this.modalityService.trainingsUsing(this.exercise);
		
		for(Training training:trainings){
			if(!training.isTrainingSystem()){
				this.showErrorMessage(	this.localizedText("ErrorOnChange"),
										this.localizedText("TheExerciseIsUsedOnTraining") + " " +
										training.getName());
			}
		}
		
	}
	
	
	@Override
	public void onVersionChanged(ValueChangeEvent e){
		this.refreshExercise();
	}
	
	@Override
	public void refresh(){
		
		this.refreshExercise();
	}

	
	
	
	private void refreshExercise(){
		this.equipmentTable.setEquipments(this.exercise.getEquipments());
		this.photosListHandler.setPhotoType(PhotosListCompInterface.EXERCISE_PHOTO);
		this.photosListHandler.setPhotos(this.exercise.getPhotos());
		this.videosListHandler.setVideoType(VideosListCompInterface.EXERCISE_VIDEO);
		this.videosListHandler.setVideos(this.exercise.getVideos());
		this.bodyRegions = this.modalityService.listBodyRegions();

		this.izList.setModality(this.modality);
		this.izList.setExercise(this.exercise);


		this.izList.refresh();
		this.equipmentTable.refresh();
		this.photosListHandler.refresh();
		this.videosListHandler.refresh();
		
	}
	
	@Override
	public void deleteMuscle(){
		if(this.exerciseMuscle != null){
			this.exercise.getExerciseMuscles().remove(this.exerciseMuscle);
		}
	}
		
	@Override
	public void handleBodyRegionChange(){
		if(this.selBodyRegion != null){
			this.muscleGroups = this.selBodyRegion.getMuscleGroups();
		}
	}
	
	@Override
	public void handleMuscleGroupChange(){
		if(this.selMuscleGroup != null){
			this.muscles = this.selMuscleGroup.getMuscles();
		}
	}


	@Override
	public void addMuscle(){
		ExerciseMuscle em = new ExerciseMuscle(this.musclePrimary,this.exercise,this.selMuscle);
		this.exercise.getExerciseMuscles().add(em);
	}
		
	
	@Override
	public void changeAllExerciseExecutions(org.primefaces.event.ToggleSelectEvent ev){
		if(ev.isSelected()){
			this.exercise.getExecutions().addAll(this.modality.getExecutions());
		}else{
			this.exercise.getExecutions().clear();
		}
	}	
		
	
	@Override
	public VideosListCompInterface getVideosListHandler() {
		return videosListHandler;
	}

	@Override
	public void setVideosListHandler(VideosListCompInterface videosListHandler) {
		this.videosListHandler = videosListHandler;
	}

	@Override
	public PhotosListCompInterface getPhotosListHandler() {
		return photosListHandler;
	}

	@Override
	public void setPhotosListHandler(PhotosListCompInterface photosListHandler) {
		this.photosListHandler = photosListHandler;
	}

	@Override
	public EquipmentTableCompInterface getEquipmentTable() {
		return equipmentTable;
	}

	@Override
	public void setEquipmentTable(EquipmentTableCompInterface equipmentTable) {
		this.equipmentTable = equipmentTable;
	}

	@Override
	public List<Exercise> getExercises() {
		return exercises;
	}

	@Override
	public void setExercises(List<Exercise> exercises) {
		this.exercises = exercises;
	}

	@Override
	public Exercise getExercise() {
		return exercise;
	}

	@Override
	public void setExercise(Exercise exercise) {
		this.exercise = exercise;
	}

	@Override
	public Modality getModality() {
		return modality;
	}

	@Override
	public void setModality(Modality modality) {
		this.modality = modality;
	}

	@Override
	public List<BodyRegion> getBodyRegions() {
		return bodyRegions;
	}

	@Override
	public void setBodyRegions(List<BodyRegion> bodyRegions) {
		this.bodyRegions = bodyRegions;
	}

	@Override
	public List<MuscleGroup> getMuscleGroups() {
		return muscleGroups;
	}

	@Override
	public void setMuscleGroups(List<MuscleGroup> muscleGroups) {
		this.muscleGroups = muscleGroups;
	}

	@Override
	public List<Muscle> getMuscles() {
		return muscles;
	}

	@Override
	public void setMuscles(List<Muscle> muscles) {
		this.muscles = muscles;
	}

	@Override
	public BodyRegion getSelBodyRegion() {
		return selBodyRegion;
	}

	@Override
	public void setSelBodyRegion(BodyRegion selBodyRegion) {
		this.selBodyRegion = selBodyRegion;
	}

	@Override
	public MuscleGroup getSelMuscleGroup() {
		return selMuscleGroup;
	}

	@Override
	public void setSelMuscleGroup(MuscleGroup selMuscleGroup) {
		this.selMuscleGroup = selMuscleGroup;
	}

	@Override
	public Muscle getSelMuscle() {
		return selMuscle;
	}

	@Override
	public void setSelMuscle(Muscle selMuscle) {
		this.selMuscle = selMuscle;
	}

	@Override
	public Muscle getMuscle() {
		return muscle;
	}

	@Override
	public void setMuscle(Muscle muscle) {
		this.muscle = muscle;
	}

	@Override
	public ExerciseMuscle getExerciseMuscle() {
		return exerciseMuscle;
	}

	@Override
	public void setExerciseMuscle(ExerciseMuscle exerciseMuscle) {
		this.exerciseMuscle = exerciseMuscle;
	}

	public ModalityServiceInterface getModalityService() {
		return modalityService;
	}

	public void setModalityService(ModalityServiceInterface modalityService) {
		this.modalityService = modalityService;
	}

	@Override
	public boolean isMusclePrimary() {
		return musclePrimary;
	}

	@Override
	public void setMusclePrimary(boolean musclePrimary) {
		this.musclePrimary = musclePrimary;
	}

	@Override
	public IntensityZoneListCompInterface getIzList() {
		return izList;
	}

	@Override
	public void setIzList(IntensityZoneListCompInterface izList) {
		this.izList = izList;
	}

	@Override
	public Exercise getSelectedVersion() {
		return selectedVersion;
	}

	@Override
	public void setSelectedVersion(Exercise selectedVersion) {
		this.selectedVersion = selectedVersion;
	}

	@Override
	public VersionableExercise getVersionableExercise() {
		return versionableExercise;
	}

	@Override
	public void setVersionableExercise(VersionableExercise versionableExercise) {
		this.versionableExercise = versionableExercise;
	}

	@Override
	public ExerciseDetailDelegateInterface getDelegate() {
		return delegate;
	}

	@Override
	public void setDelegate(ExerciseDetailDelegateInterface delegate) {
		this.delegate = delegate;
	}

}
